package com.example.kotlincomponent

import android.app.DatePickerDialog
import android.app.Dialog
import android.app.TimePickerDialog
import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.DatePicker
import android.widget.EditText
import android.widget.TimePicker
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import com.example.utils.BaseActivity
import kotlinx.android.synthetic.main.activity_dialog.*
import java.lang.StringBuilder
import java.time.Instant
import java.time.LocalDateTime
import java.util.*

class DialogActivity : BaseActivity(), View.OnClickListener {
	private val TAG = "DialogActivity"

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_dialog)
		btn_dialog_22.setOnClickListener(this)
		btn_dialog_2.setOnClickListener(this)
		btn_dialog_6.setOnClickListener(this)
		btn_dialog_7.setOnClickListener(this)
	}

	//显示一般对话框 即：确定取消框
	fun showAD(v: View) {
		AlertDialog.Builder(this).run {
			setTitle("删除数据")//设置标题
			setMessage("确定删除数据吗？")
			setIcon(R.drawable.ic_launcher_foreground)
			setCancelable(false)

			setPositiveButton("OK") { dialog, which ->
				dialog.dismiss()
				Toast.makeText(this.context, "确定", Toast.LENGTH_SHORT).show()
			}
			setNegativeButton("Cancel") { dialog, which ->
				dialog.dismiss()
				Toast.makeText(this.context, "取消", Toast.LENGTH_SHORT).show()
			}

			setNeutralButton("不确定") { dialog, which ->
				dialog.dismiss()
				Toast.makeText(this.context, "不确定", Toast.LENGTH_SHORT).show()
			}
			show()
		}
	}

	override fun onClick(v: View?) {
		when (v?.id) {
			R.id.btn_dialog_2 -> {
				Log.d(TAG, "onClick: ")
				//带单选列表的对话框
				AlertDialog.Builder(this).run {
					setTitle("指定背景颜色")
//                    setMessage("1")
					//设置单选列表
					setSingleChoiceItems(
						arrayOf("1", "2", "#"), 0
					) { _, _ ->
						Toast.makeText(this.context, "1", Toast.LENGTH_SHORT).show()
					}
					show()
				}
			}

			R.id.btn_dialog_22 -> {
				//多选列表对话框
				val selected = BooleanArray(5) { i -> false }
				val array = Array(5) { i -> (i + 1).toString() }
				AlertDialog.Builder(this).run {
					setTitle("多选列表对话框")
					setMultiChoiceItems(
						array,
						null
					) { dialog, which, checked ->
						if (checked) {
							Toast.makeText(this.context, "第${which}项被选中了", Toast.LENGTH_SHORT)
								.show()

						} else
							Toast.makeText(this.context, "第${which}项没有被选中", Toast.LENGTH_SHORT)
								.show()

						selected[which] = checked
					}

					setPositiveButton("提交") { dialog, which ->
						val stringBuilder = StringBuilder()
						for (i in 0 until 5) {
							stringBuilder.append("${array[i]} :${selected[i]} \n")
						}
						Log.d(TAG, "选中情况为：${stringBuilder}")
					}
					show()
				}
			}

			R.id.btn_dialog_3 -> {
				//动态加载布局文件 得到对应的View对象
				//view 的真实对象是布局文件根标签的类型
				//如何得到一个独立的View的子View？
				val view = View.inflate(this, R.layout.dialog_view, null)
				val nameET = view.findViewById<EditText>(R.id.et_dialog_name)
				val namePD = view.findViewById<EditText>(R.id.et_dialog_pwd)
				val username = nameET.text.toString()
				val pwd = namePD.text.toString()

//                AlertDialog.Builder(this)
//                        .setView(view)
//                        .setNegativeButton("取消") { dialog, which -> }
//                        .setPositiveButton("确定",object:DialogInterface.OnClickListener{
//                            override fun onClick(dialog: DialogInterface?, which: Int) {
//                                Toast.makeText((Context)view,username+"-"+pwd,Toast.LENGTH_SHORT)
//                            }
//
//                        })
//                        .show()


			}

			R.id.btn_dialog_6 -> {
				val calendar = Calendar.getInstance()
				val year = calendar.get(Calendar.YEAR)
				val monthOfYear = calendar.get(Calendar.MONTH)
				val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
				Log.d(TAG, "$year-$monthOfYear-$dayOfMonth")
				//DatePickerDialog
				DatePickerDialog(this,
					{ view, year, month, dayOfMonth -> //                        TODO("Not yet implemented")
						Log.d(TAG, "$year-$month-$dayOfMonth")
					}, year, monthOfYear, dayOfMonth).show()
			}

			R.id.btn_dialog_7 -> {
				//TimePickerDialog
				val calendar = Calendar.getInstance()
				val hour = calendar.get(Calendar.HOUR_OF_DAY)
				val minute = calendar.get(Calendar.MINUTE)
				Log.d(TAG, "$hour:$minute")
				TimePickerDialog(this,
					{ view, hourOfDay, minute -> //                        TODO("Not yet implemented")
						Log.d(TAG, "$hourOfDay:$minute")
					}, hour, minute, true
				).show()
			}


		}
	}
}